Ext.define('Lms.rms.HardwareInfoDialog',{
	openWindow : function(config) {
		Ext.apply(this, config);
		this.isEditableMode = false; 
		var title = LocaleUtil.getMessage('rms.label.hardware');
		if(typeof(config) != 'undefined'){
			title += LocaleUtil.getMessage('rms.label.detail');
			this.isEditableMode = true;
		}else{
			title += LocaleUtil.getMessage('reg');
		}
		this.dialog = Common.openDialog(title, 500, 500, [{
			text : (this.isEditableMode)?LocaleUtil.getMessage('update'):LocaleUtil.getMessage('reg'),
			scope : this,
			handler : function(){
				if(!this.form.isValid()) return;
				Common.confirm(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.msg.doYouWantToSave'),function(btn){
					if(btn == 'yes'){
						var url;
						if(this.isEditableMode){
							//수정 logic
							url = 'hardware/update.do';
						}else{
							//등록 logic
							url = 'hardware/create.do';
						}
						this.form.submit({
							url : url,
							params : this.form.getForm().getValues(),
							waitMsg: LocaleUtil.getMessage('rms.msg.waitToSaving'),
							scope : this,
							success : function(form, action){
								this.dialog.close();
								Ext.getCmp('HardwareMgmPanel').getStore().reload();
							},
							failure : Common.failureMessageSubmit
						});
//						Common.formSubmit({
//							url : url,
//							params : this.form.getValues(),
//							reloadPanelId : 'HardwareMgmPanel',
//							closeDialog : this.dialog
//						},this);
					}
				},this);
			}
		},{
			text : LocaleUtil.getMessage('close'),
			cls : 'blue_button',
			scope : this,
			handler : function() {
				if(!this.isEditableMode && this.manualStore.getTotalCount() > 0){
					this.deleteManualByParentId();
				}
				this.dialog.close();
				Ext.getCmp('HardwareMgmPanel').getStore().reload();
			}
		}]);
		this.dialog.resizable = true;
//		this.dialog.iconCls = 'detail_search';
		this.dialog.add(Ext.widget('panel',{
			xtype : 'panel',
			layout : 'border',
			border : false,
			frame : false,
			items : [
			    this.getForm(),
			    this.getManualGrid()
			]
		}));
		this.dialog.show();
		if(this.isEditableMode){
			this.getData();
		}else{
			this.setNewId();
			
		}
	},
	
	setNewId : function(){
		Ext.Ajax.request({
	        url:'common/getNewId.do',
	        scope : this,
	        success : function(result, request){
	        	this.id = result.responseText;
	        	this.form.getForm().findField('id').setValue(result.responseText);
	        },
	        failure: Common.failureMessage
	    });  
	},
	
	getData : function(){
		this.form.load({
			url : 'hardware/findInfo.do',
			params : {
				id : this.id
			}
		});
		this.manualStore.load({
			params : {
				parentId : this.id
			}
		});
	},
	
	getForm : function(){
		var store = new Ext.data.JsonStore({
			autoLoad:false,						
			fields: ['type'],
			proxy : {
				type : 'ajax',
				url : 'hardware/getHardwareTypes.do'
			}
		});
		
		this.form = Ext.widget('form',{
		    frame : false,
		    border : false,
		    region : 'north',
		    bodyPadding: '5 5 0',
	        defaults: {anchor: '100%'},
	        defaultType: 'textfield',
	        fieldDefaults: {
	        	labelAlign : 'right',
                labelWidth: 120,
                msgTarget : 'side'
            },
	        items :[{
	        		xtype : 'hiddenfield',
	        		name : 'id'
	        	},{            	
                    fieldLabel: LocaleUtil.getMessage('rms.label.hwName'),
                    name: 'name',
                    allowBlank : false,
                    readOnly : false,
                    style:{textTransform: 'uppercase'}
                },{
                	xtype : 'combo',
                	store : store,
    				name: 'type',    				
    				fieldLabel: LocaleUtil.getMessage('rms.label.type'),
    				valueField: 'type',
    	          	displayField:'type',
    				readOnly : false
    			},{
                	xtype : 'numberfield',
                	fieldLabel: LocaleUtil.getMessage('rms.label.inventory'),
                    name: 'inventory',
                    minValue : 1,
                    value : 1,
                    allowBlank : false,
                    readOnly : false
                },{
                	xtype : 'textarea',
                	fieldLabel: LocaleUtil.getMessage('rms.label.description'),
                    name: 'description',
                    grow      : true,
                    readOnly : false
                },{
                	xtype : 'filefield',                		
                    fieldLabel: LocaleUtil.getMessage('rms.label.manual'),
                    name: 'manualFile',
                    readOnly : true,
                    buttonCfg: {
                        iconCls: 'save'
                    },
                    scope : this,
                    listeners : {
                    	scope : this,
                    	'change' : this.createManual
                    }
                }
            ]      
		});
		
		return this.form;
	},
	
	createManual : function(){
		if(!this.form.getForm().findField('manualFile').getValue()) return;
		if(!this.form.isValid()) return;
		this.form.getForm().submit({
			url : 'manual/create.do',
			scope : this,
			waitMsg: LocaleUtil.getMessage('rms.msg.uploadingToFile'),
			success : function(form, action){
				this.manualStore.load({
					params : {
						parentId : this.id
					}
				});
			},
			failure : Common.failureMessageSubmit
		});
//		Common.formSubmit({
//			url : url,
//			params : this.form.getValues(),
//			reloadPanelId : 'HardwareMgmPanel',
//			closeDialog : this.dialog
//		},this);
	},
	
	getManualGrid : function(){
		Ext.define('ManualModel', {
	        extend: 'Ext.data.Model',	        
	        fields: [
	            'id','parentId', 'sepType', 'fileName'
	        ],
	        idProperty: 'id'
	    });
		
		this.manualStore = Ext.create('Ext.data.Store', {
			model : 'ManualModel',
			autoLoad: false,
			sortInfo: {
		        field    : 'fileName',
		        direction: 'ASC'
		    },
		    proxy : {
		    	type : 'ajax',
		    	url : 'manual/getManualByParentId.do',
		    	params : {
					parentId : this.id
				},
		    	reader : {
		    		type : 'json'
		    	}
		    }
		});
		this.manualGrid = Ext.create('Ext.grid.Panel', {
			region : 'center',
		    store: this.manualStore,
		    loadMask: true,
			hideHeaders : true,
			selType: 'checkboxmodel',
			columnLines: true,
			viewConfig : Common.viewConfig(true),
		    columns: [
		        {text : 'FileName',dataIndex : 'fileName',  flex: 1 },
		        {
		        	xtype : 'actioncolumn',
		        	width: 25,
		        	sortable: false,
	                menuDisabled: true,
	                items: [{
	                    iconCls: 'save',
	                    scope: this,
	                    handler : this.downloadManual
	                }]
		        },{
		        	xtype : 'actioncolumn',
		        	width: 25,
		        	sortable: false,
		        	menuDisabled: true,
		        	items: [{
		        		iconCls: 'delete',
		        		scope: this,
		        		handler : this.deleteManualById
		        	}]
		        }
		    ]
		});
		return this.manualGrid;
	},
	
	downloadManual : function(grid, rowIndex, colIndex){
		var selectedRecord = grid.getStore().getAt(rowIndex);
		Common.downloadIframe('manual/downloadManual.do?id='+selectedRecord.get('id'));
	},
	
	deleteManualById : function(grid, rowIndex, colIndex){
		var selectedRecord = grid.getStore().getAt(rowIndex);
		Common.confirm(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.msg.doYouWantToDeleteSelectedFile'),function(btn){
			if(btn == 'yes'){
				Ext.Ajax.request({
					url:'manual/delete.do',
					params: {
						id : selectedRecord.get('id')
					},
					scope : this,
					success : function(result, request){
						this.manualStore.reload();
					},
					failure: Common.failureMessage
				});
			}
		},this);
	},
	
	deleteManualByParentId : function(){
		Ext.Ajax.request({
			url:'manual/deleteByParentId.do',
			params: {
				id : this.id
			},
			scope : this,
			failure: Common.failureMessage
		});
	}
});